package kubernetes

import (
	"context"
	kongConfigurationAPIV1 "github.com/kong/kubernetes-ingress-controller/pkg/apis/configuration/v1"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	ev1 "knative.dev/eventing/pkg/apis/eventing/v1"
	sourcev1 "knative.dev/eventing/pkg/apis/sources/v1"
	kapiV1 "knative.dev/serving/pkg/apis/serving/v1"
)

func (in *FcClient) CreateKnativeService(namespace string, service *kapiV1.Service) (*kapiV1.Service, error) {
	return in.KServing.Services(namespace).Create(context.TODO(), service, emptyCreateOptions)
}

func (in *FcClient) UpdateKnativeService(namespace string, service *kapiV1.Service) (*kapiV1.Service, error) {
	return in.KServing.Services(namespace).Update(context.TODO(), service, emptyUpdateOptions)
}

func (in *FcClient) GetKnativeRevisionList(namespace string, labelSelector string) (*kapiV1.RevisionList, error) {
	options := metav1.ListOptions{
		LabelSelector: labelSelector,
	}
	return in.KServing.Revisions(namespace).List(context.TODO(), options)
}

func (in *FcClient) GetKnativeService(namespace, serviceName string) (*kapiV1.Service, error) {
	options := metav1.GetOptions{}
	return in.KServing.Services(namespace).Get(context.TODO(), serviceName, options)
}

func (in *FcClient) DeleteKnativeService(namespace, serviceName string) error {
	return in.KServing.Services(namespace).Delete(context.TODO(), serviceName, emptyDeleteOptions)
}

func (in *FcClient) DeleteKnativeRevision(namespace, revisionName string) error {
	return in.KServing.Revisions(namespace).Delete(context.TODO(), revisionName, emptyDeleteOptions)
}

func (in *FcClient) CreateKnativeRoute(namespace string, route *kapiV1.Route) (*kapiV1.Route, error) {
	return in.KServing.Routes(namespace).Create(context.TODO(), route, emptyCreateOptions)
}

func (in *FcClient) GetKnativeRoute(namespace, name string) (*kapiV1.Route, error) {
	return in.KServing.Routes(namespace).Get(context.TODO(), name, emptyGetOptions)
}

func (in *FcClient) UpdateKnativeRoute(namespace string, route *kapiV1.Route) (*kapiV1.Route, error) {
	return in.KServing.Routes(namespace).Update(context.TODO(), route, emptyUpdateOptions)
}

func (in *FcClient) DeleteKnativeRoute(namespace, name string) error {
	return in.KServing.Routes(namespace).Delete(context.TODO(), name, emptyDeleteOptions)
}

func (in *FcClient) GetKongClusterPlugin(name string) (*kongConfigurationAPIV1.KongClusterPlugin, error) {
	return in.kongConfig.KongClusterPlugins().Get(context.TODO(), name, emptyGetOptions)
}

func (in *FcClient) UpdateKongClusterPlugin(config *kongConfigurationAPIV1.KongClusterPlugin) (*kongConfigurationAPIV1.KongClusterPlugin, error) {
	return in.kongConfig.KongClusterPlugins().Update(context.TODO(), config, emptyUpdateOptions)
}

func (in *FcClient) DeleteKnativeRouteByLabel(namespace string, labelSelector string) error {
	listOptions := metav1.ListOptions{
		LabelSelector: labelSelector,
	}
	return in.KServing.Routes(namespace).DeleteCollection(context.TODO(), emptyDeleteOptions, listOptions)
}

func (in *FcClient) CreatePingsource(namespace string, ps *sourcev1.PingSource) (*sourcev1.PingSource, error) {
	return in.KEventing.SourcesV1().PingSources(namespace).Create(context.TODO(), ps, emptyCreateOptions)
}

func (in *FcClient) UpdatePingsource(namespace string, ps *sourcev1.PingSource) (*sourcev1.PingSource, error) {
	return in.KEventing.SourcesV1().PingSources(namespace).Update(context.TODO(), ps, emptyUpdateOptions)
}

func (in *FcClient) CreateTrigger(namespace string, t *ev1.Trigger) (*ev1.Trigger, error) {
	return in.KEventing.EventingV1().Triggers(namespace).Create(context.TODO(), t, emptyCreateOptions)
}

func (in *FcClient) UpdateTrigger(namespace string, t *ev1.Trigger) (*ev1.Trigger, error) {
	return in.KEventing.EventingV1().Triggers(namespace).Update(context.TODO(), t, emptyUpdateOptions)
}

func (in *FcClient) CreateKongPlugin(namespace string, kp *kongConfigurationAPIV1.KongPlugin) (*kongConfigurationAPIV1.KongPlugin, error) {
	return in.kongConfig.KongPlugins(namespace).Create(context.TODO(), kp, emptyCreateOptions)
}

func (in *FcClient) UpdateKongPlugin(namespace string, kp *kongConfigurationAPIV1.KongPlugin) (*kongConfigurationAPIV1.KongPlugin, error) {
	return in.kongConfig.KongPlugins(namespace).Update(context.TODO(), kp, emptyUpdateOptions)
}

func (in *FcClient) DeleteKongPlugin(namespace string, kp string) error {
	return in.kongConfig.KongPlugins(namespace).Delete(context.TODO(), kp, emptyDeleteOptions)
}
